package com.redstars.generate.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.redstars.generate.convert.SysProjectConvert;
import com.redstars.generate.dao.SysProjectDao;
import com.redstars.generate.entity.SysProject;
import com.redstars.generate.enums.ProjectDefaultEnum;
import com.redstars.generate.exception.RedstarsException;
import com.redstars.generate.page.PageResult;
import com.redstars.generate.query.SysProjectQuery;
import com.redstars.generate.service.SysProjectService;
import com.redstars.generate.utils.PageUtils;
import com.redstars.generate.vo.SysProjectVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * @author : zhouhx
 * @date : 2023/3/20 16:50
 */
@Service
@AllArgsConstructor
public class SysProjectServiceImpl extends ServiceImpl<SysProjectDao, SysProject> implements SysProjectService {
    @Override
    public PageResult<SysProjectVo> page(SysProjectQuery query) {
        LambdaQueryWrapper<SysProject> wrapper = getWrapper(query);
        PageUtils<SysProject> pageUtils = new PageUtils<>();
        IPage<SysProject> page = this.page(pageUtils.getPage(query), wrapper);

        return pageUtils.getPageData(page, SysProjectVo.class);
    }


    private LambdaQueryWrapper<SysProject> getWrapper(SysProjectQuery query){
        LambdaQueryWrapper<SysProject> wrapper = Wrappers.lambdaQuery();
        if(StrUtil.isNotBlank(query.getProjectName())){
            wrapper.like(SysProject::getProjectName,query.getProjectName());
        }
        return wrapper;
    }

    @Override
    public void save(SysProjectVo vo) {
        SysProject entity = SysProjectConvert.INSTANCE.convert(vo);
        if(checkProjectDefault(vo)){
            throw new RedstarsException("已经存在默认项目");
        }
        baseMapper.insert(entity);
    }

    @Override
    public void update(SysProjectVo vo) {
        SysProject entity = SysProjectConvert.INSTANCE.convert(vo);
        if(checkProjectDefault(vo)){
            throw new RedstarsException("已经存在默认项目");
        }
        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }
    @Override
    public SysProjectVo detail(Long id) {
        SysProject detail = this.getById(id);
        if (ObjectUtil.isEmpty(detail)) {
            throw new RedstarsException("记录不存在");
        }
        return BeanUtil.copyProperties(detail,SysProjectVo.class);
    }

    @Override
    public boolean checkProjectDefault(SysProjectVo vo) {
        LambdaQueryWrapper<SysProject> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SysProject::getIsDefault, ProjectDefaultEnum.DEFALUT.getStatus());
        List<SysProject> list = this.list(queryWrapper);

        Integer isDefault = vo.getIsDefault();
        if(CollUtil.isNotEmpty(list)){
            if(ProjectDefaultEnum.DEFALUT.getStatus().equals(isDefault) && !list.get(0).getId().equals(vo.getId())){
                throw new RedstarsException("已经存在默认项目");
            }

        }
        return false;
    }

    @Override
    public List<SysProjectVo> list(SysProjectQuery query) {
        LambdaQueryWrapper<SysProject> wrapper = getWrapper(query);
        wrapper.orderByAsc(SysProject::getCreateTime);
        List<SysProject> list = list(wrapper);
        return  SysProjectConvert.INSTANCE.convertList(list);
    }

    @Override
    public SysProjectVo getDefalut() {
        LambdaQueryWrapper<SysProject> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SysProject::getIsDefault, ProjectDefaultEnum.DEFALUT.getStatus());
        SysProject project = this.getOne(queryWrapper);
        return SysProjectConvert.INSTANCE.convert(project);
    }
}
